﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Libs.Content;

public partial class Controls_Right_Vote : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            init();
        }
    }

    private void init()
    {
        var _Vote = new Votes();
        List<Votes> list;

        list = _Vote.GetList();
        _Vote = list[0];

        lblTitle.Text = _Vote.Title;
        hlResult.NavigateUrl = "/votes.result.html?id=" + _Vote.VoteID.ToString();

        var _VoteQuestion = new VoteQuestion();
        rptList.DataSource = _VoteQuestion.GetList(_Vote.VoteID);
        rptList.DataBind();
    }

    protected void btVote_Click(object sender, EventArgs e)
    {
        if (Session["Vote"] != null)
        {
            Response.Redirect(hlResult.NavigateUrl);
        }

        for (int i = 0; i < rptList.Items.Count; i++)
        {
            CheckBox cbxCheck = (CheckBox)rptList.Items[i].FindControl("cbxCheck");
            Label lblQuestionID = (Label)rptList.Items[i].FindControl("lblQuestionID");

            if (cbxCheck.Checked)
            {
                var _VoteQuestion = new VoteQuestion();
                _VoteQuestion.QuestionID = Convert.ToInt32(lblQuestionID.Text);
                _VoteQuestion = _VoteQuestion.Get();
                _VoteQuestion.Count = _VoteQuestion.Count + 1;
                _VoteQuestion.Update();
                break;
            }
        }
        Session["Vote"] = "1";
        Response.Redirect(hlResult.NavigateUrl);
    }
}